iT邦幫忙

2025 iThome 鐵人賽

DAY 3
0

今日內容:數學、printf、字串函式、子字串


數學

不需要引入任何標頭檔
全部都與之前學的C++的寫法相同,只是需要加前綴(Math.)

System.out.println(Math.PI); // 輸出系統預設的PI值(3.141592653589793)
System.out.println(Math.E); // 輸出系統預設的尤拉數值(2.718281828459045)

double result;
result = Math.pow(2, 3); // 2^3 - >8.0
result = Math.abs(-5); // 絕對值 -> 5.0
result = Math.sqrt(9); // 開根號 -> 3.0
result = Math.round(3.45); // 直接小數後一位四捨五入 -> 3.0
result = Math.ceil(3.14); // 取上高斯 -> 4.0
result = Math.floor(3.98); // 取下高斯 -> 3.0
result = Math.max(10, 20); // 取最大值 -> 20.0
result = Math.min(10, 20); // 取最小值 -> 10.0

printf

不用引入標頭檔
用於格式化輸出,與C學過的的printf使用方式相同

System.out.printf("%s is %d years old, %fcm, employeed: %b", "Kyle", 21, 183.2, true);
// Kyle is 21 years old, 183.2cm, employeed: true

對於浮點數,%f會預設為輸出至六位小數

System.out.printf("%f", price);    // 輸出至第六位
System.out.printf("%.2f", price1); // 輸出至第二位
System.out.printf("%.9f", price2); // 輸出至第九位

對於輸出的數字縮排,方法與過去相同

System.out.printf("%04d", 1);   // -> 0001
System.out.printf("%4d", 12);   // ->   12
System.out.printf("%-4d", 123); // -> 123 

以下是在Java中新學到的地方
%+:如果想要在輸出時,正數前面有"+"號,而負數照常輸出"-"

System.out.printf("%+.3f", price3); // 假設price3 = 10.5 -> +10.500
System.out.printf("%+.4f", price4); // 假設price4 = -54.1 -> -54.1000

%,:每千位自動加逗號

System.out.printf("%,d", price5); // 假設price5 = 1234578 -> 1,234,578

%(:如果輸出負數會自動加括號並省略負號

System.out.printf("%(d", price6); // 假設price6 = -21 -> (21)

% (空格):正數前面輸出空格,負數前面輸出負號

System.out.printf("% d", price7); // 假設price7 = 1543 ->  1543;
System.out.printf("% d", price8); // 假設price8 = -543 -> -543;

字串函式

String str = "Hello World";

str.length(); // 計算字串長度,回傳int | 對應到C++中的 .length()
str.charAt(int index); // 字串在index的字元,回傳char | .at() 
str.indexOf(char ch); // 找到第一個字元ch所在的index,回傳int | .find()
str.lastIndexOf(char ch); // 找到最後一個字元ch所在的index,回傳int | .rfind()
str.isEmpty(); // 確認字串是否為空,回傳bool | .empty()
str.contains(String st); // 字串中尋找是否包含子字串st(也可為空格),回傳bool
str.equals(String st); // 確認字串str是否等於st,回傳bool
str.equalsIgnoreCase(String st); // 同equals,但不考慮字母大小寫,回傳bool
    
str = str.toUpperCase(); // 整個字串全部轉成大寫
str = str.toLowerCase(); // 整個字串全部轉成小寫
str = str.trim(); // 將字串中 有字元前的空格,與字元結尾以後的空格全部刪除
// "   Hello World    " -> "Hello World"
str = str.replace("S", "R"); // 將字串中的S全部換成R,可以是字串,不一定是替換字元

子字串

String email = "Bro123@gmail.com";
String username = email.substring(0, 6); // .substring(起始位置, 終止位置(開區間))
String domain = email.substring(7); // 也可忽略終止位置,即直接到最後一個字元
// username -> "Bro123" , domain -> "@gmail.com"

以上是手動輸入,但如果要應用在情況會變換的方式如下:

String email = "Aabbccdd@gmail.com";
String username = email.substring(0, email.indexOf("@"));
String domain = email.substring(email.indexOf("@") + 1); // +1是為了忽略'@'

結語

今天是學習Java基本語法的第二天,遇到了還算熟悉的math跟printf,比較困難的可能是字串的函式,雖然效果會一樣但是語法不同,需要另外記。
感覺還是很順利的學完了今天的目標,明天繼續!/images/emoticon/emoticon08.gif


上一篇
Day 2:Java基本語法(一)
下一篇
Day 4:Java基本語法(三)
系列文
30天從基礎學起Java,直到做出我的第一個遊戲21
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言